<%- content_for :title do -%>
  カレンダー
<%- end -%>

<%- content_for :head do -%>
<%= stylesheet_link_tag 'tasks', :media => :all %>
<%= javascript_include_tag 'prototype' %>
<%= javascript_include_tag 'common' %>
<script type="text/javascript">
  Event.observe(window, 'load', function(event) {
    var teamSelect = $('team-select');
    teamSelect.observe('change', function(event) {
      location.href = location.href.addQueryParams({
        team: teamSelect.value,
        member: 'all'
      });
    });

    var memberSelect = $('member-select');
    memberSelect.observe('change', function(event) {
      location.href = location.href.addQueryParams({
        member: memberSelect.value
      });
    });

  });
</script>
<%- end -%>

<%= render :partial => 'navigation' %>

<% if @view == :all || @member %>

<div class="calendar">
  <div class="header">
    <div class="row">
      <div class="cell"><div class="cell-content">月</div></div>
      <div class="cell"><div class="cell-content">火</div></div>
      <div class="cell"><div class="cell-content">水</div></div>
      <div class="cell"><div class="cell-content">木</div></div>
      <div class="cell"><div class="cell-content">金</div></div>
      <div class="cell saturday"><div class="cell-content">土</div></div>
      <div class="cell sunday"><div class="cell-content">日</div></div>
    </div>
  </div>
  <div class="body">
    <%- @week_tasks.each do |week| -%>
    <div class="cell-header">
      <div class="row">
        <%- day = week[:first_day] -%>
        <%- 7.times do -%>
        <div class="<%=h day_cell_classes(day, @date).join(' ') %>">
          <div class="date"><%= link_to (day.day == 1 ? sprintf('%d月 1日', day.month) : day.day), new_task_url('task[formatted_begin_at]' => day.strftime('%Y/%m/%d'), 'task[formatted_end_at]' => day.strftime('%Y/%m/%d')), { :title => 'この日のタスクを作成する' } %></div>
        </div>
        <%- day = day.tomorrow -%>
        <%- end -%>
      </div>
    </div>
    <div class="cell-content">
      <div class="row">
        <div class="tasks">
          <%- week[:task_rows].each do |task_row| -%>
          <div class="task-row">
            <div class="task-spacer">&nbsp;</div>
            <%- task_row.each do |task| -%>
            <div class="<%=h task_classes(task, week[:first_day]).join(' ') %>">
              <div class="task-content">
                <%= link_to h(task.title), task_url(task.task) %>
              </div>
            </div>
            <%- end -%>
          </div>
          <%- end -%>
        </div>
        <%- if week[:task_rows].size > 0 -%>
        <div class="cells">
          <%- day = week[:first_day] -%>
          <%- 7.times do -%>
          <div class="<%= day_cell_classes(day, @date).join(' ') %>">
            <%- week[:task_rows].size.times do -%>
            <div class="task-spacer">&nbsp;</div>
            <%- end -%>
          </div>
          <%- day = day.tomorrow -%>
          <%- end -%>
        </div>
        <%- end -%>
      </div>
    </div>
    <%- end -%>
  </div>
  <div class="footer"></div>
</div>

<% else %>

<div class="calendar member-calendar">
  <div class="header">
    <div class="row">
      <div class="cell member-cell"><div class="cell-content">&nbsp;</div></div>
      <div class="cell"><div class="cell-content">月</div></div>
      <div class="cell"><div class="cell-content">火</div></div>
      <div class="cell"><div class="cell-content">水</div></div>
      <div class="cell"><div class="cell-content">木</div></div>
      <div class="cell"><div class="cell-content">金</div></div>
      <div class="cell saturday"><div class="cell-content">土</div></div>
      <div class="cell sunday"><div class="cell-content">日</div></div>
    </div>
  </div>
  <div class="body">
    <%- @week_tasks.each do |week| -%>
    <%- reset_cycle() -%>
    <div class="cell-header">
      <div class="row">
        <div class="cell member-cell"></div>
        <%- day = week[:first_day] -%>
        <%- 7.times do -%>
        <div class="<%=h day_cell_classes(day, @date).join(' ') %>">
          <div class="date"><%= link_to (day.day == 1 ? sprintf('%d月 1日', day.month) : day.day), new_task_url('task[formatted_begin_at]' => day.strftime('%Y/%m/%d'), 'task[formatted_end_at]' => day.strftime('%Y/%m/%d')), { :title => 'この日のタスクを作成する' } %></div>
        </div>
        <%- day = day.tomorrow -%>
        <%- end -%>
      </div>
    </div>
    <%- next unless week[:member_rows] -%>
    <%- week[:member_rows].each do |member_row| -%>
    <%- member = member_row[:member] -%>
    <div class="cell-content">
      <div class="row <%=h cycle('even', 'odd') %>-row">
        <div class="tasks">
          <%- member_row[:task_rows].each do |task_row| -%>
          <div class="task-row">
            <div class="task-spacer">&nbsp;</div>
            <%- task_row.each do |task| -%>
            <div class="<%=h task_classes(task, week[:first_day]).join(' ') %>">
              <div class="task-content">
                <%= link_to h(task.title), task_url(task.task) %>
              </div>
            </div>
            <%- end -%>
          </div>
          <%- end -%>
        </div>
        <%- if member_row[:task_rows].size > 0 -%>
        <div class="cells">
          <div class="cell member-cell">
            <div class="member-cell-content">
              <%= link_to h(member.name), link_params(:member => member.id)  %>
            </div>
            <%- member_row[:task_rows].size.times do -%>
            <div class="task-spacer">&nbsp;</div>
            <%- end -%>
          </div>
          <%- day = week[:first_day] -%>
          <%- 7.times do -%>
          <div class="<%=h day_cell_classes(day, @date).join(' ') %>">
            <%- member_row[:task_rows].size.times do -%>
            <div class="task-spacer">&nbsp;</div>
            <%- end -%>
          </div>
          <%- day = day.tomorrow -%>
          <%- end -%>
        </div>
        <%- end -%>
      </div>
    </div>
    <%- end -%>
    <%- end -%>
  </div>
  <div class="footer"></div>
</div>

<% end %>